package com.easthome.controller;

import com.easthome.common.RespResult;
import com.easthome.entity.Goods;
import com.easthome.entity.UserInfo;
import com.easthome.pojo.vo.CartVO;
import com.easthome.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @RequestMapping("/addToCart")
    public RespResult addToCart(@RequestBody Goods goods, @SessionAttribute UserInfo user){
        System.out.println("addToCart..."+goods);
        int r = cartService.addToCart(goods,user.getUserId());
        return RespResult.success();
    }

    @RequestMapping("/getCart")
    public RespResult getCart(@SessionAttribute UserInfo user){//session.setAtrribute("user",user)
        System.out.println("getCart..."+user);
        //查询购物车列表
        CartVO cartVO = cartService.getCart(user.getUserId());//List<CartItemVO>  CartVO
        return RespResult.success(cartVO);
    }

    @PutMapping("/updateCart/{goodsId}/{amount}")
    public RespResult updateCart(@SessionAttribute UserInfo user, @PathVariable Integer goodsId,@PathVariable Integer amount){//session.setAtrribute("user",user)
        System.out.println("updateCart..."+user+","+goodsId+","+amount);
        //修改购物车信息
        int r = cartService.updateCart(user.getUserId(),goodsId,amount);
        return RespResult.success();
    }

    @DeleteMapping("/removeCartItem/{goodsId}")
    public RespResult removeCartItem(@SessionAttribute UserInfo user, @PathVariable Integer goodsId){//session.setAtrribute("user",user)
        System.out.println("removeCartItem..."+user+","+goodsId);
        //修改购物车信息
        int r = cartService.removeCartItem(user.getUserId(),goodsId);
        return RespResult.success();
    }
}
